package com.jinxin.platform.cdperprotypay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysHouse;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysOrg;
import com.jinxin.platform.cdperprotypay.pojo.domain.RoomUser;
import com.jinxin.platform.cdperprotypay.pojo.form.RoomDetailForm;
import com.jinxin.platform.cdperprotypay.pojo.form.SendMsgForm;
import com.jinxin.platform.cdperprotypay.pojo.vo.DictEntity;
import com.jinxin.platform.cdperprotypay.support.ModularSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : zhangpeng
 * @date : 2020-07-07 16:27:18
 **/
@Slf4j
@RestController
@RequestMapping("cdpropertypay/modular")
public class ModularSupportController {
    @Resource
    private ModularSupport modularSupport;


    @PostMapping("/getVillage")
    public JsonResult getVillage(){ //获取小区信息
        List<SysOrg> sysOrg = modularSupport.getSysOrg();
//        List<SysOrg> sysOrgs = new ArrayList<>();
//        for (SysOrg s:sysOrg) {
//            if(s.getOrgLevels().equals("1")){
//                sysOrgs.add(s);
//            }
//        }
        log.info("获取小区列表：{}",sysOrg);
        return JsonResult.ok(sysOrg);
    }

    @PostMapping("/getUnit")
    public JsonResult getUnit(String buildId){ //获取单元信息
        Map map = new HashMap<>();
        map.put("buildId",buildId);
        List<SysHouse> unit = modularSupport.getUnit(map);
        return JsonResult.ok(unit);
    }

    @PostMapping("/getBuild")
    public JsonResult getBuild(String villageId){ //获取楼栋信息
        Map map = new HashMap<>();
        map.put("villageId",villageId);
        List<SysHouse> unit = modularSupport.getBuild(map);
        return JsonResult.ok(unit);
    }

    @PostMapping("/getRoom")
    public JsonResult getRoom(SysHouse sysHouse){ //获取房间信息
        Map map = new HashMap<>();
        if(sysHouse.getUnitId() != null){
            map.put("unitId",sysHouse.getUnitId());
        }
        if(sysHouse.getBuildId() != null){
            map.put("buildId",sysHouse.getBuildId());
        }
        map.put("villageId",sysHouse.getVillageId());
        List<SysHouse> room = modularSupport.getAllHouse(map);
        return JsonResult.ok(room);
    }

    @PostMapping("/getRoomUser")
    public JsonResult getRoomUser(RoomDetailForm detailForm){ //获取房间居住人员信息
        Map map = new HashMap<>();
        map.put("roomId",detailForm.getRoom());
        List<RoomUser> user = modularSupport.getUserByRId(map);
        return JsonResult.ok(user);
    }

    @PostMapping("/getRoomDetail")
    public JsonResult getRoomDetail(String roomId){ //获取房间信息
        Map map = new HashMap<>();
        map.put("roomId",roomId);
        SysHouse room = modularSupport.getRoomById(map);
        return JsonResult.ok(room);
    }

    @GetMapping("/getLoginUser")
    public JsonResult getLoginUser(){
        return JsonResult.ok(modularSupport.getCurrentUser());
    }

    @GetMapping("/getRoomByUser")
    public JsonResult getRoomByUser(){
        return JsonResult.ok(modularSupport.getRoomByUser());
    }

    @PostMapping("/sendMsg")
    public JsonResult sendMsg(SendMsgForm sendMsgForm){
        List<SendMsgForm> formList = JSONObject.parseArray(sendMsgForm.getReceive(), SendMsgForm.class);
        String fasong = "";
        for (SendMsgForm s:formList) {
            HashMap<String,String> map = new HashMap<>();
            map.put("receivePhone",s.getUserMobile());
            map.put("smsType","1");
            map.put("businessModule","物业缴费");
            map.put("receiveUser",s.getUserName());
            map.put("param",s.getUserName()+","+s.getChargeItem());
            fasong = modularSupport.fasong(map);
        }

        return JsonResult.ok(JSONObject.parse(fasong));
    }

    @PostMapping("modelData")
    public void modelData(@RequestBody String modelData){
        log.info("--------------获取到的APP模块列表：{}-------------------", modelData);
    }

    @GetMapping("getDic")
    public String getDic(){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code","domainName");
        List<DictEntity> dict = modularSupport.getDict(map);
        return dict.get(0).getDictValue();
    }


}
